package com.ruoyi.system.exception;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.exception.ex.ApprovalException;
import com.ruoyi.system.exception.ex.CrudException;
import com.ruoyi.system.exception.ex.DataParameterException;
import com.ruoyi.system.exception.ex.UploadException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.sql.SQLException;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州学院-计算机与信息工程学院
 * @ClassName GlobalExceptionHandler
 * @Description 全局异常处理类
 * @Date 2022/07/06 9:21
 */
@ControllerAdvice
public class MyGlobalExceptionHandler {


    private static final Logger log = LoggerFactory.getLogger(MyGlobalExceptionHandler.class);


    /**
     * 大的异常
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult exception(Exception e)
    {
        log.error("异常" + e.getMessage(), e);
        return AjaxResult.error("未知错误！");
    }

    /**
     * sql异常
     */
    @ExceptionHandler(SQLException.class)
    public AjaxResult sqlException(SQLException e)
    {
        log.error("sql语句异常" + e.getMessage(), e);
        return AjaxResult.error("未知错误！");
    }

    /**
     * 增删改查操作异常
     */
    @ExceptionHandler(CrudException.class)
    public AjaxResult crudException(CrudException e)
    {
        log.error("增删改查异常" + e.getMessage(), e);
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 文件上传操作异常
     */
    @ExceptionHandler(UploadException.class)
    public AjaxResult uploadException(UploadException e)
    {
        log.error("文件上传异常" + e.getMessage(), e);
        return AjaxResult.error(e.getMessage());
    }


    /**
     * 审批异常
     */
    @ExceptionHandler(ApprovalException.class)
    public AjaxResult approvalException(ApprovalException e)
    {
        log.error("市场产品审批异常" + e.getMessage(), e);
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 数据参数异常
     */
    @ExceptionHandler(DataParameterException.class)
    public AjaxResult dataParameterException(ApprovalException e)
    {
        log.error("数据参数异常" + e.getMessage(), e);
        return AjaxResult.error(e.getMessage());
    }

}
